package com.liuzhilong.infrastructure.basic.command.hystrix;

import com.netflix.hystrix.*;

public class RemoteCommand extends HystrixCommand<String> {

    private LongtimeRemoteService longtimeRemoteService;
    public RemoteCommand(LongtimeRemoteService longtimeRemoteService){
        super(Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey("orderService"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("queryByOrderId"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerRequestVolumeThreshold(10)//至少有10个请求，熔断器才进行错误率的计算
                        .withCircuitBreakerSleepWindowInMilliseconds(5000)//熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
                        .withCircuitBreakerErrorThresholdPercentage(50)//错误率达到50开启熔断保护
                        .withExecutionTimeoutEnabled(true).withExecutionTimeoutInMilliseconds(3*1000))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties
                        .Setter().withCoreSize(10)));
        this.longtimeRemoteService = longtimeRemoteService;
    }

    @Override
    protected String run()  {
        return longtimeRemoteService.getData();
    }
    @Override
    protected String getFallback(){
        return "error";
    }


}
